<?php

namespace backend\modules\bonus\forms;

use Yii;
use yii\base\Model;
use yii\web\NotFoundHttpException;
use common\models\common\Config;
use common\models\bonus\Bonus;
use common\models\bonus\BonusYear;
use common\models\bonus\BonusQi;
use common\models\bonus\BonusDp;


class DpForm extends Model
{

    public $bonus_tax_fb_amount;
    public $bonus_tax_fb_pro;
    public $bonus_tax_hz_amount;
    public $bonus_tax_hz_pro;
    public $bonus;
    public $fee;
    public $tax;
    public $payment;
    public $zbb;
    public $nbb;

    public function init()
    {
        $this->bonus_tax_fb_amount = Yii::$app->debris->backendConfig('bonus_tax_fb_amount');
        $this->bonus_tax_fb_pro = Yii::$app->debris->backendConfig('bonus_tax_fb_pro');
        $this->bonus_tax_hz_amount = Yii::$app->debris->backendConfig('bonus_tax_hz_amount');
        $this->bonus_tax_hz_pro = Yii::$app->debris->backendConfig('bonus_tax_hz_pro');
        $res = Bonus::duiPeng(false);
        $this->bonus = $res['bonus'];
        $this->fee = $res['fee'];
        $this->tax = $res['tax'];
        $this->payment = $res['payment'];
        $yeji = BonusYear::find()->sum('sales');
        if (!$yeji) {
            $yeji = 0;
        }
        $bs = BonusQi::find()->select('sum(payment) as sf')->asArray()->one();
        $payment = 0;
        if ($bs) {
            $payment = $bs['sf'];
        }
        $payment += $this->payment;
        $zbb = '-';
        if ($yeji > 0) {
            $zbb = intval($payment / $yeji * 10000) / 100;
        }
        $this->zbb = $zbb . '%';
        $model = BonusDp::addYeji();
        $yj = $model->ye_ji;
        $nbb = '-';
        if ($yj && $yj > 0) {
            $nbb = intval($this->payment / $yj * 10000) / 100;
        }
        $this->nbb = $nbb . '%';
        return parent::init();
    }

    public function rules()
    {
        return [
            [['bonus_tax_fb_amount', 'bonus_tax_fb_pro', 'bonus_tax_hz_amount', 'bonus_tax_hz_pro'], 'string'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'bonus_tax_fb_amount' => '分布奖管理费多扣起步奖金',
            'bonus_tax_fb_pro' => '分布奖管理费额外扣除比例',
            'bonus_tax_hz_amount' => '合作奖管理费多扣起步奖金',
            'bonus_tax_hz_pro' => '合作奖管理费额外扣除比例 ',
            'bonus' => '预计总奖金',
            'tax' => '预计税费',
            'fee' => '预计管理费',
            'payment' => '预计实发',
            'nbb' => '预计本次拨比',
            'zbb' => '预计结算后总拨比',
        ];
    }

    public function reset()
    {
        $nameArr = [
            'bonus_tax_fb_amount',
            'bonus_tax_fb_pro',
            'bonus_tax_hz_amount',
            'bonus_tax_hz_pro',
        ];
        foreach ($nameArr as $key => $name) {
            $model = Config::findOne(['name'=>$name]);
            if ($model) {
                $value = $model->value;
                $value->data = $this->$name;
                $value->save();
            }
        }
        Yii::$app->debris->backendConfigAll(true);
        return true;
    }



}